package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.settings.domain.DictionaryValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.utile.DateTimeUtil;
import com.bjpowernode.crm.utile.UUIDUtil;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.domain.ClueRemark;
import com.bjpowernode.crm.workbench.domain.ClueRemarkRelation;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ClueRelationService;
import com.bjpowernode.crm.workbench.service.ClueRemarkService;
import com.bjpowernode.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:ClueContoller
 * package:com.bjpowernode.crm.workbench.web.controller
 * Description:
 *
 * @date: 2022-06-11 22:26
 * @author：lin
 */
@Controller
@RequestMapping("/workbench/clue")
public class ClueController {

    @Autowired
    private UserService userService;

    @Autowired
    private DicService dicService;

    @Autowired
    private ClueService clueService;

    @Autowired
     private ClueRemarkService remarkService;

    @Autowired
    private  ActivityService activityService;

    @Autowired
    private  ClueRelationService  relationService;



    //跳转线索页面，并将线索页面所需数据查询并返回前台
    //user 称呼 线索状态、线索来源、线索 （字典表）
    @RequestMapping("/toIndex.do")
    public String toIndex(Model model){

    List<User> userList= userService.getUser();
    List<DictionaryValue>appellation =dicService.searchDicValue("appellation");
    List<DictionaryValue> clueStateList =dicService.searchDicValue("clueState");
    List<DictionaryValue>sourceList =dicService.searchDicValue("source");
    model.addAttribute("appellation",appellation);
    model.addAttribute("clueStateList",clueStateList);
    model.addAttribute("sourceList",sourceList);
    model.addAttribute("userList",userList);




        return "/workbench/clue/index";
    }

    //	url:"workbench/clue/createClue.do",
    //保存交易线索
    @RequestMapping("/createClue.do")
    @ResponseBody
    public Map<String,Object> saveClue(Clue clue, HttpSession session){
        User user= (User) session.getAttribute("user");
        clue.setId(UUIDUtil.getUUID());
        clue.setCreateBy(user.getName());
        clue.setCreateTime(DateTimeUtil.getSysTime());

       boolean success= clueService.saveClue(clue);

       Map<String,Object> map = new HashMap<>();
       map.put("success",success);

       return map;



    }

    //		url:"workbench/clue/getPageClue.do",
    //查询交易信息
    @RequestMapping("/getPageClue.do")
    @ResponseBody
    public  Map<String,Object> searchClue(Clue clue,String pageNo,String pageSize ){
        Map<String,Object> map =new HashMap<>();
        int pageNoSer = Integer.valueOf(pageNo);
        int pageSizeSer = Integer.valueOf(pageSize);
        int skipCount =(pageNoSer-1)*pageSizeSer;
        map.put("clue",clue);
        map.put("skipCount",skipCount);
        map.put("pageSizeSer",pageSizeSer);

        Map<String,Object> pageMap= clueService.searchClue(map);

        return pageMap;


    }

    //跳转交易页面window.location.href='workbench/clue/toDetail.do
    //并获取数据
    @RequestMapping("/toDetail.do")
    public String toDetail(String id , Model model){
        Clue clue =clueService.searchClueById(id);
        List<ClueRemark> remarkList =remarkService.searchClueRemarkById(id);
        List<Activity> activityList = activityService.activityByClueId(id);
        model.addAttribute("clue",clue);
        model.addAttribute("remarkList",remarkList);
        model.addAttribute("activityList",activityList);




        return "/workbench/clue/detail";
    }

    //   url:"workbench/clue/activityAll.do",
    //获取所有的市场活动数据，返回关联表模态窗口，
    @RequestMapping("/activityAll.do")
    @ResponseBody
    public List<Activity> activityAll(String id,String name){
        Map map =new HashMap();
        map.put("clueId",id);
        map.put("name",name);

      List<Activity> activityList=  activityService.activityAll(map);

      map.put("activityList",activityList);
        return activityList;


    }
    //		url:"workbench/clue/relationActivity.do",               //请求路径
    //关联市场活动
    @RequestMapping("/relationActivity.do")
    @ResponseBody
    public Map<String,Object> relationActivity(String[] activityId,String clueId ){

       Map<String,Object> map =new HashMap<>();
        List<ClueRemarkRelation> list = new ArrayList<>();
        ClueRemarkRelation relation=null;
        for (String s : activityId) {
            relation=new ClueRemarkRelation();
            relation.setActivityId(s);
            relation.setClueId(clueId);
            relation.setId(UUIDUtil.getUUID());

            list.add(relation);
        }


        boolean success=relationService.saveRelationActivity(list);
        List<Activity> activityList=  activityService.activityByClueId(clueId);
        map.put("success",success);
        map.put("activityList",activityList);


        return map;



    }

    //	url:"workbench/clue/deleteClueActRelation.do",  //请求路径
    //  删除交易的所关联的市场活动-
    @RequestMapping("/deleteClueActRelation.do")
    @ResponseBody
    public Map<String,Object>  deleteClueActRelation(ClueRemarkRelation relation){
    boolean  success =relationService.deleteClueActRelation(relation);
    Map map =new HashMap();

    map.put("success",success);
    return map;




    }





}
